package com.chenglu.android.logistics.mvp.presenter;





import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.net.RxScheduler;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.logistics.bean.LogisticsBean;
import com.chenglu.android.logistics.mvp.contract.LogisticsContract;
import com.chenglu.android.logistics.mvp.model.LogisticsModel;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;


/**
 * @author: lyj
 * @Created by Copyright 2023/10/17 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 *
 */
public class LogisticsPresenter extends BasePresenter<LogisticsContract.View> implements LogisticsContract.LogisticsPresenter {

     private  LogisticsContract.LogisticsModel model;

    public LogisticsPresenter(){
        model = new LogisticsModel();
    }

    @Override
    public void getOrderLogistics(String token,String orderNo) {

        if(!isViewAttached()){
            return;
        }

        model.getOrderLogistics(token,orderNo)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<LogisticsBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull LogisticsBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getOrderLogistics(bean);
                                break;
                            case Constant.HTTPCODE400:
                                mView.noLogistics(bean.getMsg());
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE401:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE404:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                          mView.dismissLoading();
                          ToastUtils.show(e.getMessage());
                          LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }

}
